package com.sskj.api.controller.admin;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sskj.api.domain.Device;
import com.sskj.api.service.IDeviceService;
import com.sskj.api.vo.req.DeviceReq;
import com.sskj.api.vo.req.PageReq;
import com.sskj.common.dto.DeviceService;
import com.sskj.common.dto.DeviceProperty;
import com.sskj.common.util.ApiResponseUtil;
import com.sskj.common.vo.ApiResponse;
import com.sskj.common.vo.SimplePagination;
import com.sskj.common.vo.req.DeviceCommandReq;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/device")
@Tag(name = "设备模块")
@Validated
public class DeviceController {

    @Autowired
    private IDeviceService deviceService;

    @Operation(summary = "新增")
    @PostMapping("add")
    public ApiResponse<Object> add(@Valid @RequestBody Device device) {
        deviceService.add(device);

        return ApiResponseUtil.success();
    }

    @Operation(summary = "删除")
    @PostMapping("deleteById")
    public ApiResponse<Object> deleteById(@RequestParam Long id) {
        deviceService.deleteById(id);

        return ApiResponseUtil.success();
    }

    @Operation(summary = "更新")
    @PostMapping("update")
    public ApiResponse<Object> update(@Valid @RequestBody Device device) {
        deviceService.update(device);

        return ApiResponseUtil.success();
    }

    @Operation(summary = "分页查询")
    @PostMapping("pageList")
    public ApiResponse<SimplePagination<Device>> pageList(DeviceReq req) {
        IPage<Device> deviceIPage = deviceService.pageList(req);

        return ApiResponseUtil.success(deviceIPage);
    }

    @Operation(summary = "设备详情")
    @GetMapping("detail")
    public ApiResponse<Device> detail(Long id) {
        Device detail = deviceService.detail(id);
        return ApiResponseUtil.success(detail);
    }

    @Operation(summary = "获取设备属性信息")
    @GetMapping("getDeviceProperties")
    public ApiResponse<List<DeviceProperty>> getDeviceProperties(Long id) {
        List<DeviceProperty> deviceProperties = deviceService.getDeviceProperties(id);

        return ApiResponseUtil.success(deviceProperties);
    }

    @Operation(summary = "获取设备事件列表")
    @GetMapping("getDeviceServices")
    public ApiResponse<List<DeviceService>> getDeviceServices(Long id) {
        List<DeviceService> deviceServices = deviceService.getDeviceServices(id);

        return ApiResponseUtil.success(deviceServices);
    }

    @Operation(summary = "发送指令")
    @PostMapping("sendCommand")
    public ApiResponse<Object> sendCommand(DeviceCommandReq req) {
        Object res = deviceService.sendCommand(req);

        return ApiResponseUtil.success(res);
    }

    @Operation(summary = "我的设备分页列表")
    @GetMapping("myDevices")
    public ApiResponse<SimplePagination<Device>> myDevices(PageReq req) {
        long userId = StpUtil.getLoginIdAsLong();
        IPage<Device> deviceIPage = deviceService.myDevices(userId, req.getPageNum(), req.getPageSize());

        return ApiResponseUtil.success(deviceIPage);
    }

//    @Operation(summary = "播放视频")
//    @GetMapping("playVideo")
//    public ApiResponse<Map<String, String>> playVideo(PlayVideoReq req) {
//        if (req.getChannelNumber() == null) {
//            req.setChannelNumber(1);
//        }
//        if (req.getQualityType() == null) {
//            req.setQualityType(VideoQualityEnum.SD.getType());
//        }
//        String playUrl = deviceService.playVideo(req.getId(), req.getChannelNumber(), req.getQualityType());
//        Map<String, String> data = new HashMap<>();
//        data.put("playUrl", playUrl);
//        return ApiResponseUtil.success(data);
//    }
}
